//
//  YTPriceBonusViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/19.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTPriceBonusViewController.h"
#import "YTPriceBonusViewCell.h"
#import "YTPriceBonusModel.h"
#import "YTUserProfileServices.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface YTPriceBonusViewController ()<UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *segmentView;
@property (strong, nonatomic) NSMutableArray<YTPriceBonusModel *> *dataArray;
@end

@implementation YTPriceBonusViewController

- (void)initialize {
    _tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    _dataArray = [NSMutableArray array];
    [_tableView registerNib:[UINib nibWithNibName:@"YTPriceBonusViewCell" bundle:nil] forCellReuseIdentifier:@"YTPriceBonusViewCell"];
    [self createSegment];
    [self loadDataWithType:1];
}

- (void)createSegment {
    YTSegmentedControlConfig *config = [[YTSegmentedControlConfig alloc] init];
    config.indecatorWidth = 60;
    config.titleSize = 19;
    YTSegmentedControl *segmentControl = [[YTSegmentedControl alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)
                                                         titles:@[@"未使用", @"已使用", @"已过期"]
                                                  configuration:config];
    @weakify(self);
    segmentControl.didSelectedItem = ^(NSInteger index) {
        @strongify(self);
        [self loadDataWithType:index + 1];
    };
    [self.segmentView addSubview:segmentControl];
}

- (void)loadDataWithType:(NSInteger)type {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"Status"] = [NSString stringWithFormat:@"%zd", type];
    [[YTUserProfileServices bonusList:dict] subscribeNext:^(NSDictionary *response) {
        [SVProgressHUD dismiss];
        NSDictionary *responseData = response[@"responseData"];
        _dataArray = [[NSArray yy_modelArrayWithClass:[YTPriceBonusModel class] json:responseData[@"couponKeyList"]] mutableCopy];
        [_tableView reloadData];
    }];
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YTPriceBonusViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTPriceBonusViewCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

#pragma mark - DZNEmptyDataSetSource

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"暂无相关优惠券～";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0f],
                                 NSForegroundColorAttributeName: kGrayColor};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"empy_bonus"];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}

@end
